//
//  BrandDetailViewController.m
//  UGoods
//
//  Created by dllo on 16/1/22.
//  Copyright © 2016年 蓝鸥科技.李贺. All rights reserved.
//

#import "BrandDetailViewController.h"
#import "FindModel.h"
#import "BaseTableView.h"
#import <UIImageView+WebCache.h>
#import "LHAFNetworkTool.h"
#import "BrandDetailBrandCell.h"
#import "BrandDetailBuyerCell.h"
#import "BaseLabel.h"
#import "BaseCollectionView.h"
#import "BaseView.h"
#import "GoodsCollectionViewCell.h"
#import "BuyDetailViewController.h"
#import "ConstantsDefine.h"
@interface BrandDetailViewController ()<UITableViewDataSource, UITableViewDelegate, UICollectionViewDataSource, UICollectionViewDelegate>

@property (nonatomic, retain) BaseTableView *tableView;
@property (nonatomic, retain) NSMutableArray *dataArray;
@property (nonatomic, retain) NSDictionary *brandInfoDic;
@property (nonatomic, retain) NSMutableArray *goodsArray;
@property (nonatomic, retain) BaseCollectionView *collectionView;

@end

@implementation BrandDetailViewController

- (void)handleData {
    NSString *str = [NSString stringWithFormat:@"http://api.ujipin.com/v2/app/ios/phone/v3/list/index?channel=appstore&idfa=1EF25F55-6367-4567-B0D5-82DF4F1557CD&user_id=470825&plantform=iphone&version=314&brand=%@&page_size=20&page=1", _model.brand_id];
    self.brandInfoDic = [NSDictionary dictionary];
    [LHAFNetworkTool getUrl:str body:nil response:LHJSON requestHeadFile:nil success:^(NSURLSessionDataTask *task, id resposeObject) {
        self.brandInfoDic = resposeObject[@"data"][@"brandInfo"];
        NSArray *array = resposeObject[@"data"][@"goods_list"];
        self.goodsArray = [FindModel modelHandleWithArray:array];
        
        UIImageView *headerImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 200 * HEIGHT)];
        [headerImageView sd_setImageWithURL:[NSURL URLWithString:_brandInfoDic[@"app_show_header"]] placeholderImage:[UIImage imageNamed:@"place"]];
        self.tableView.tableHeaderView = headerImageView;
        
        self.navigationItem.title = _brandInfoDic[@"brand_name"];
        [self.tableView reloadData];
        [self creteFooterCollection];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"请求失败：%@", error);
    }];
}

- (void)creteFooterCollection {
    BaseView *backView = [[BaseView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 260 * HEIGHT * ((_goodsArray.count + 1) / 2) + 80 * HEIGHT)];
    backView.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
    self.tableView.tableFooterView = backView;
    [backView release];
    
    BaseLabel *alabel = [[BaseLabel alloc] initWithFrame:CGRectMake(10 * WIDTH, 10 * HEIGHT, 100 * WIDTH, 20 * HEIGHT)];
    alabel.text = @"明星款式";
    alabel.font = [UIFont systemFontOfSize:15 * HEIGHT];
    [backView addSubview:alabel];
    [alabel release];
    
    UICollectionViewFlowLayout *layout = [[[UICollectionViewFlowLayout alloc] init] autorelease];
     layout.itemSize = CGSizeMake((self.view.frame.size.width - 30 * WIDTH) / 2, 250 * HEIGHT);
    layout.minimumInteritemSpacing = 10 * WIDTH;
//    layout.minimumLineSpacing = 10;
    layout.sectionInset = UIEdgeInsetsMake(0, 10 * WIDTH, 0, 10 * WIDTH);
    layout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
    
    self.collectionView = [[BaseCollectionView alloc] initWithFrame:CGRectMake(0, 40 * WIDTH, self.view.frame.size.width, backView.frame.size.height - 80 * HEIGHT) collectionViewLayout:layout];
    self.collectionView.backgroundColor = [UIColor colorWithWhite:0.950 alpha:1.000];
    _collectionView.delegate = self;
    _collectionView.dataSource = self;
//    self.collectionView.showsHorizontalScrollIndicator = NO;
    //        self.collectionView.bounces = NO;
    [backView addSubview:_collectionView];
    [_collectionView release];
    [_collectionView registerClass:[GoodsCollectionViewCell class] forCellWithReuseIdentifier:@"header"];
}

// collectionViewcell 个数
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return _goodsArray.count;
}

// collectionViewcell设置
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    GoodsCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"header" forIndexPath:indexPath];
    FindModel *model = _goodsArray[indexPath.item];
    cell.model = model;
    cell.layer.borderWidth = 1;
    cell.layer.borderColor = [UIColor colorWithWhite:0.902 alpha:1.000].CGColor;
    return cell;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSLog(@"%@", indexPath);
    BuyDetailViewController *buy = [[BuyDetailViewController alloc] init];
    FindModel *model = _goodsArray[indexPath.item];
    buy.model = model;
    [self.navigationController pushViewController:buy animated:YES];
}

- (void)createTableView {
    self.tableView = [[BaseTableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    [_tableView release];
    [_tableView registerClass:[BrandDetailBrandCell class] forCellReuseIdentifier:@"cell1"];
    [_tableView registerClass:[BrandDetailBuyerCell class] forCellReuseIdentifier:@"cell2"];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        BrandDetailBrandCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        [cell.logoImageView sd_setImageWithURL:[NSURL URLWithString:_brandInfoDic[@"app_show_logo"]] placeholderImage:[UIImage imageNamed:@"place2"]];
        cell.brandNameLabel.text = [NSString stringWithFormat:@"[%@]%@", _brandInfoDic[@"site_url"], _brandInfoDic[@"brand_name"]];
        cell.positionLabel.text = _brandInfoDic[@"brand_position"];
        cell.descLabel.text = _brandInfoDic[@"brand_desc"];
        return cell;
    } else if (indexPath.row == 1) {
        BrandDetailBuyerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        [cell.buyerImageView sd_setImageWithURL:[NSURL URLWithString:_brandInfoDic[@"buyer_tj"][@"img"]] placeholderImage:[UIImage imageNamed:@"place2"]];
        cell.buyerNameLabel.text = _brandInfoDic[@"buyer_tj"][@"uname"];
        cell.briefLabel.text = _brandInfoDic[@"buyer_tj"][@"brief"];
        return cell;
    } else {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        }
        return cell;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        CGFloat h = [BrandDetailBrandCell heightWithText:_brandInfoDic[@"brand_desc"]];
        return h * HEIGHT + 80 * HEIGHT;
    } else if (indexPath.row == 1) {
        CGFloat h = [BrandDetailBuyerCell heightWithText:_brandInfoDic[@"buyer_tj"][@"brief"]];
        return h * HEIGHT + 80 * HEIGHT;
    } else {
        return 50 * HEIGHT;
    }
}


- (void)setModel:(FindModel *)model {
    if (_model != model) {
        [_model release];
        _model = [model retain];
    }
    [self handleData];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self createTableView];
}


@end
